<template>
  <div>
    <!--  class="edp-home-dict-init1" -->
    <el-card class="aui-card--fill" :body-style="{padding: '10px'}">
      <div slot="header" class="clearfix">
        <span class="edp-home-dict-init-card-header-title" style="font-size: 16px;">国标数据初始化</span>
      </div>
      <div class="edp-home-dict-init">
        <el-row :gutter="10">
          <el-col v-for="item in dataList" :key="item.id" :span="6" style="padding-top: 10px;" @click="typeChange(item)">
            <el-card class="edp-home-dict-init-card" shadow="hover">
              <div slot="header" class="clearfix">
                <span class="edp-home-dict-init-card-header-title" style="cursor:pointer" @click="typeChange(item)">{{item.dictName}}({{item.dataNum}})</span>
                <div class="edp-home-dict-init-card-header-right">
                  <!-- <el-tooltip class="item" effect="dark" content="初始化" placement="bottom"> -->
                  <el-tag v-if="!item.status" style="margin-left: 10px;" @click="dictInit(item)"><i class="el-icon-circle-plus-outline" style="cursor:pointer"></i></el-tag>
                  <el-tag v-if="item.status" type="info" style="margin-left: 10px;"><i class="el-icon-circle-plus-outline"></i></el-tag>
                </div>
              </div>
              <div class="edp-home-dict-init-card-body">
                <!-- <el-form label-width="40px"> -->
                <el-row :gutter="20">
                  <el-col :span="12">
                    <!-- <el-form-item label="状态" prop="status"> -->
                    <el-tag v-if="item.status" type="success">已初始化</el-tag>
                    <el-tag v-else type="danger">未初始化</el-tag>
                    <!-- </el-form-item> -->
                  </el-col>
                  <el-col :span="12" style="float: right;">
                    <el-button type="text" style="float: right;" @click="typeChange(item)">查看</el-button>
                  </el-col>
                </el-row>
                <!-- </el-form> -->
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <dict-init-data v-if="dialogVisible" ref="dictInitData"></dict-init-data>
    </el-card>
  </div>
</template>
<script>

import DictInitData from './dict-init-data'

export default {
  data () {
    return {

      dialogVisible: false,
      categoryList: [],
      dictType: []

    }
  },
  components: {
    // AddOrUpdate
    DictInitData
  },
  computed: {
    dataList () {
      let data = []
      for (const categorys of this.categoryList) {
        for (const cat of categorys.dictTypeList) {
          cat.status = this.dictType[cat.dictType]
          data.push(cat)
        }
      }
      return data
    }
  },
  created () {
    this.getCategoryList()
    this.getAllTypeList()
  },
  methods: {
    typeChange (item) {
      this.dialogVisible = true
      this.$nextTick(() => {
        console.log('dictInitData', this.$refs.dictInitData)
        this.$refs.dictInitData.data = item
        this.$refs.dictInitData.dataForm.dictType = item.dictType
        this.$refs.dictInitData.init()
      })
    },
    // 获取信息
    getCategoryList () {
      return this.$http.get('/tmp/tmpDictTypeInit/categoryList', { params: { pid: 0 } }).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.categoryList = res.data
      }).catch(() => { })
    },
    // 获取所有类别
    getAllTypeList () {
      return this.$http.get('/tmp/tmpDictTypeInit/getAllType').then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dictType = res.data
      }).catch(() => { })
    },
    // 表单提交
    dictInit (item) {
      this.$confirm('是否初始化字典？', '提示', {
        confirmButtonText: '初始化',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let postData = this.dictType
        postData[item.dictType] = true
        console.debug(postData)
        this.$http['post']('/tmp/tmpDictTypeInit/init', postData).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.getCategoryList()
              this.getAllTypeList()
            }
          })
        }).catch(() => { })
      }).catch(() => {

      })
    }
  }
}
</script>

<style lang="scss" >
.edp-home-dict-init {
  &-card {
    .el-card__header {
      height: 40px;
      padding: 10px 10px;
      line-height: 20px;
    }
    &-header {
      &-title {
        font-size: 13px;
        float: left;
        color: #515a6e;
      }

      &-right {
        float: right;
      }
    }

    &-body {
      color: #515a6e;

      > span {
        font-size: 30px;
      }

      &-txt {
        height: 30px;
        padding-top: 8px;
      }
    }

    &-bottom {
      width: autopx;
      height: auto;
    }
  }

  &-module {
    color: #515a6e;

    &-ico {
      text-align: center;
      font-size: 32px;
      color: rgb(105, 192, 255);
    }

    &-name {
      text-align: center;
      padding-top: 20px;
    }
  }

  // ::v-deep .el-card__body {
  //   padding: 10px 10px;
  // }

  // ::v-deep .el-divider--horizontal {
  //   margin: 5px 0;
  // }
}

// .api-body {
// }
</style>
